// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"log"
	"path"
	"strings"
)

import (
	"walk"
	. "walk/declarative"
)

func main() {
	mw := new(MyMainWindow)
	var openAction *walk.Action

	if _, err := (MainWindow{
		AssignTo: &mw.MainWindow,
		Title:    "Walk Image Viewer Example",
		MenuItems: []MenuItem{
			Menu{
				Text: "&File",
				Items: []MenuItem{
					Action{
						AssignTo:    &openAction,
						Text:        "&Open",
						Image:       "../img/open.png",
						OnTriggered: mw.openAction_Triggered,
					},
					Separator{},
					Action{
						Text:        "Exit",
						OnTriggered: func() { mw.Close() },
					},
				},
			},
			Menu{
				Text: "&Help",
				Items: []MenuItem{
					Action{
						Text:        "About",
						OnTriggered: mw.aboutAction_Triggered,
					},
				},
			},
		},
		//ToolBar:ToolBar{
		//	MinSize:Size{100,40},
		//	Items:[]MenuItem{
		//		ActionRef{&openAction},
		//		Separator{},
		//	},
		//},
		MinSize: Size{500, 400},
		Size:    Size{800, 600},
		Layout:  VBox{MarginsZero: true},
		Children: []Widget{
			//VSpacer{
			//	MinSize: Size{0,10},
			//},
			//bug:tab控件和ToolBar控件在布局上有冲突,同时出现会有重叠
			Composite{
				MaxSize: Size{0, 50},
				Layout:  HBox{},
				Children: []Widget{
					HSpacer{},
					PushButton{
						//AssignTo: &mw.favorites,
						Text:     "收藏",
					},
				},
			},
			Composite{
				Layout:Grid{Columns:1,MarginsZero:false},
				Background:SolidColorBrush{Color: walk.RGB(151, 151, 151)},

				Children: []Widget{
					TabWidget{
						AssignTo: &mw.tabWidget,
						Pages:[]TabPage{
							{
								Background:SolidColorBrush{Color:walk.RGB(255,0,0)},
								Title:"界面1",
								Layout:  VBox{},
								Children:[]Widget{
									HSpacer{},
									PushButton{
										Text:     "休息休息",
									},
								},
							},
							{
								Background:SolidColorBrush{Color:walk.RGB(0,255,0)},
								Title:"界面2",
								Layout:  VBox{},
								Children:[]Widget{
									HSpacer{},
									PushButton{
										Text:     "测试测试",
									},
								},
							},
						},

					},
				},

			},

		},
	}.Run()); err != nil {
		log.Fatal(err)
	}
}

type MyMainWindow struct {
	*walk.MainWindow
	tabWidget    *walk.TabWidget
	prevFilePath string
}

func (mw *MyMainWindow) openAction_Triggered() {
	if err := mw.openImage(); err != nil {
		log.Print(err)
	}
}

func (mw *MyMainWindow) openImage() error {
	dlg := new(walk.FileDialog)
	//重用上一次的文件路径
	dlg.FilePath = mw.prevFilePath
	dlg.Filter = "Image Files (*.emf;*.bmp;*.exif;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.emf;*.bmp;*.exif;*.gif;*.jpeg;*.jpg;*.png;*.tiff"
	dlg.Title = "Select an Image"

	if ok, err := dlg.ShowOpen(mw); err != nil {
		return err
	} else if !ok {
		return nil
	}

	mw.prevFilePath = dlg.FilePath

	img, err := walk.NewImageFromFile(dlg.FilePath)
	if err != nil {
		return err
	}

	var succeeded bool
	defer func() {
		if !succeeded {
			img.Dispose()
		}
	}()

	page, err := walk.NewTabPage()
	if err != nil {
		return err
	}

	if page.SetTitle(path.Base(strings.Replace(dlg.FilePath, "\\", "/", -1))); err != nil {
		return err
	}
	page.SetLayout(walk.NewHBoxLayout())

	defer func() {
		if !succeeded {
			page.Dispose()
		}
	}()

	imageView, err := walk.NewImageView(page)
	if err != nil {
		return err
	}

	defer func() {
		if !succeeded {
			imageView.Dispose()
		}
	}()

	imageView.SetMode(walk.ImageViewModeShrink)

	if err := imageView.SetImage(img); err != nil {
		return err
	}

	if err := mw.tabWidget.Pages().Add(page); err != nil {
		return err
	}

	if err := mw.tabWidget.SetCurrentIndex(mw.tabWidget.Pages().Len() - 1); err != nil {
		return err
	}

	succeeded = true

	return nil
}

func (mw *MyMainWindow) aboutAction_Triggered() {
	walk.MsgBox(mw, "About", "Walk Image Viewer Example", walk.MsgBoxIconInformation)
}
